package com.matchmealpha;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class CreateAccountActivity extends Activity implements OnClickListener {

	protected EditText firstName;
	protected EditText lastName;
	protected EditText email;
	protected EditText username;
	protected EditText password;
	private Button leftButton;
	private Button rightButton;
	protected ProgressDialog loadingDialog;

	private static final String ACCOUNT_CREATION_SUCCESS = "success";
	private static final String ACCOUNT_CREATION_FAILURE = "failed";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Bundle bund = this.getIntent().getExtras();

		initLayout();
		initListener();

		username.setText(bund.getString("username"));
		password.setText(bund.getString("password"));
	}

	private void initLayout() {
		setContentView(R.layout.createaccount_view);
		firstName = (EditText) this.findViewById(R.id.CreateAccountFirstName);
		lastName = (EditText) this.findViewById(R.id.CreateAccountLastName);
		email = (EditText) this.findViewById(R.id.CreateAccountEmail);
		username = (EditText) this.findViewById(R.id.CreateAccountUsername);
		password = (EditText) this.findViewById(R.id.CreateAccountPassword);
		leftButton = (Button) this.findViewById(R.id.CreateAccountLeftButton);
		rightButton = (Button) this.findViewById(R.id.CreateAccountRightButton);
	}

	private void initListener() {
		leftButton.setOnClickListener(this);
		rightButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case (R.id.CreateAccountLeftButton):
			this.finish();
			break;
		case (R.id.CreateAccountRightButton):
			loadingDialog = ProgressDialog.show(CreateAccountActivity.this, "",
					"Loading. Please wait...", true);
			loadingDialog.show();
			new CreateAccountTask().execute(username.getText().toString(),
					password.getText().toString(), firstName.getText()
							.toString(), lastName.getText().toString(), email
							.getText().toString());
			break;
		}
	}

	private class CreateAccountTask extends AsyncTask<String, Void, Boolean> {

		protected Boolean doInBackground(String... data) {
			HttpRequest req = new HttpRequest();
			String response;
			response = req.sendPost(
					MatchMeTabWidget.MM_SERVER + "/create_user", "username="
							+ data[0] + "&" + "password=" + data[1] + "&"
							+ "firstname=" + data[2] + "&" + "lastname="
							+ data[3] + "&" + "email=" + data[4]);

			if (CreateAccountActivity.ACCOUNT_CREATION_SUCCESS.equals(response))
				return true;
			else if (CreateAccountActivity.ACCOUNT_CREATION_FAILURE
					.equals(response))
				return false;
			else
				return false;
		}

		protected void onPostExecute(Boolean result) {
			loadingDialog.cancel();
			if (result) {
				Toast.makeText(CreateAccountActivity.this, "Account Created",
						Toast.LENGTH_SHORT);
				finish();
			} else {
				Toast.makeText(CreateAccountActivity.this,
						"Account creation failed", Toast.LENGTH_SHORT);
			}
		}
	}
}
